home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch13 / Up.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-20  |  13KB  |  407 lines

  1. VERSION 5.00
  2. Begin VB.Form frmUp 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Up"
  6.    ClientHeight    =   5505
  7.    ClientLeft      =   330
  8.    ClientTop       =   735
  9.    ClientWidth     =   9060
  10.    BeginProperty Font 
  11.       Name            =   "MS Sans Serif"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   700
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    KeyPreview      =   -1  'True
  21.    LinkTopic       =   "Form1"
  22.    PaletteMode     =   1  'UseZOrder
  23.    ScaleHeight     =   5505
  24.    ScaleWidth      =   9060
  25.    Begin VB.PictureBox PPict 
  26.       AutoRedraw      =   -1  'True
  27.       Height          =   2175
  28.       Left            =   6840
  29.       ScaleHeight     =   -10
  30.       ScaleLeft       =   -5
  31.       ScaleMode       =   0  'User
  32.       ScaleTop        =   5
  33.       ScaleWidth      =   10
  34.       TabIndex        =   12
  35.       Top             =   2760
  36.       Width           =   2175
  37.    End
  38.    Begin VB.PictureBox Pict 
  39.       AutoRedraw      =   -1  'True
  40.       Height          =   2175
  41.       Index           =   0
  42.       Left            =   1200
  43.       ScaleHeight     =   -10
  44.       ScaleLeft       =   -5
  45.       ScaleMode       =   0  'User
  46.       ScaleTop        =   5
  47.       ScaleWidth      =   10
  48.       TabIndex        =   5
  49.       Top             =   0
  50.       Width           =   2175
  51.    End
  52.    Begin VB.PictureBox Pict 
  53.       AutoRedraw      =   -1  'True
  54.       Height          =   2175
  55.       Index           =   1
  56.       Left            =   3480
  57.       ScaleHeight     =   -10
  58.       ScaleLeft       =   -5
  59.       ScaleMode       =   0  'User
  60.       ScaleTop        =   5
  61.       ScaleWidth      =   10
  62.       TabIndex        =   4
  63.       Top             =   0
  64.       Width           =   2175
  65.    End
  66.    Begin VB.PictureBox Pict 
  67.       AutoRedraw      =   -1  'True
  68.       Height          =   2175
  69.       Index           =   2
  70.       Left            =   5760
  71.       ScaleHeight     =   -10
  72.       ScaleLeft       =   -5
  73.       ScaleMode       =   0  'User
  74.       ScaleTop        =   5
  75.       ScaleWidth      =   10
  76.       TabIndex        =   3
  77.       Top             =   0
  78.       Width           =   2175
  79.    End
  80.    Begin VB.PictureBox Pict 
  81.       AutoRedraw      =   -1  'True
  82.       Height          =   2175
  83.       Index           =   3
  84.       Left            =   0
  85.       ScaleHeight     =   -10
  86.       ScaleLeft       =   -5
  87.       ScaleMode       =   0  'User
  88.       ScaleTop        =   5
  89.       ScaleWidth      =   10
  90.       TabIndex        =   2
  91.       Top             =   2760
  92.       Width           =   2175
  93.    End
  94.    Begin VB.PictureBox Pict 
  95.       AutoRedraw      =   -1  'True
  96.       Height          =   2175
  97.       Index           =   4
  98.       Left            =   2280
  99.       ScaleHeight     =   -10
  100.       ScaleLeft       =   -5
  101.       ScaleMode       =   0  'User
  102.       ScaleTop        =   5
  103.       ScaleWidth      =   10
  104.       TabIndex        =   1
  105.       Top             =   2760
  106.       Width           =   2175
  107.    End
  108.    Begin VB.PictureBox Pict 
  109.       AutoRedraw      =   -1  'True
  110.       Height          =   2175
  111.       Index           =   5
  112.       Left            =   4560
  113.       ScaleHeight     =   -10
  114.       ScaleLeft       =   -5
  115.       ScaleMode       =   0  'User
  116.       ScaleTop        =   5
  117.       ScaleWidth      =   10
  118.       TabIndex        =   0
  119.       Top             =   2760
  120.       Width           =   2175
  121.    End
  122.    Begin VB.Label Label1 
  123.       Alignment       =   2  'Center
  124.       Caption         =   "Final projection"
  125.       Height          =   255
  126.       Index           =   6
  127.       Left            =   6840
  128.       TabIndex        =   13
  129.       Top             =   5040
  130.       Width           =   2175
  131.       WordWrap        =   -1  'True
  132.    End
  133.    Begin VB.Label Label1 
  134.       Alignment       =   2  'Center
  135.       Caption         =   "Original picture"
  136.       Height          =   255
  137.       Index           =   0
  138.       Left            =   1200
  139.       TabIndex        =   11
  140.       Top             =   2280
  141.       Width           =   2175
  142.       WordWrap        =   -1  'True
  143.    End
  144.    Begin VB.Label Label1 
  145.       Alignment       =   2  'Center
  146.       Caption         =   "Translate focus to origin"
  147.       Height          =   255
  148.       Index           =   1
  149.       Left            =   3480
  150.       TabIndex        =   10
  151.       Top             =   2280
  152.       Width           =   2175
  153.       WordWrap        =   -1  'True
  154.    End
  155.    Begin VB.Label Label1 
  156.       Alignment       =   2  'Center
  157.       Caption         =   "Rotate center of projection into Y-Z plane"
  158.       Height          =   495
  159.       Index           =   2
  160.       Left            =   5760
  161.       TabIndex        =   9
  162.       Top             =   2280
  163.       Width           =   2175
  164.       WordWrap        =   -1  'True
  165.    End
  166.    Begin VB.Label Label1 
  167.       Alignment       =   2  'Center
  168.       Caption         =   "Rotate center of projection into Z axis"
  169.       Height          =   375
  170.       Index           =   3
  171.       Left            =   0
  172.       TabIndex        =   8
  173.       Top             =   5040
  174.       Width           =   2175
  175.       WordWrap        =   -1  'True
  176.    End
  177.    Begin VB.Label Label1 
  178.       Alignment       =   2  'Center
  179.       Caption         =   "Rotate UP into Y-Z plane"
  180.       Height          =   255
  181.       Index           =   4
  182.       Left            =   2280
  183.       TabIndex        =   7
  184.       Top             =   5040
  185.       Width           =   2175
  186.       WordWrap        =   -1  'True
  187.    End
  188.    Begin VB.Label Label1 
  189.       Alignment       =   2  'Center
  190.       Caption         =   "Project onto X-Y plane"
  191.       Height          =   255
  192.       Index           =   5
  193.       Left            =   4560
  194.       TabIndex        =   6
  195.       Top             =   5040
  196.       Width           =   2175
  197.       WordWrap        =   -1  'True
  198.    End
  199. Attribute VB_Name = "frmUp"
  200. Attribute VB_GlobalNameSpace = False
  201. Attribute VB_Creatable = False
  202. Attribute VB_PredeclaredId = True
  203. Attribute VB_Exposed = False
  204. Option Explicit
  205. Private FirstCube As Integer
  206. ' Viewing parameters.
  207. Private EyeR As Single      ' Center of projection.
  208. Private EyeTheta As Single
  209. Private EyePhi As Single
  210. Private Const FocusX = 0#       ' Focus point.
  211. Private Const FocusY = 0#
  212. Private Const FocusZ = 0#
  213. ' Projection parameters.
  214. Private UpX As Single       ' Up vector.
  215. Private UpY As Single
  216. Private UpZ As Single
  217. Private Cx As Single        ' Center of projection.
  218. Private Cy As Single
  219. Private Cz As Single
  220. Private Fx As Single        ' Focus point.
  221. Private Fy As Single
  222. Private Fz As Single
  223. ' Matrices used for the projection.
  224. Private M(0 To 5) As Transformation
  225. Private Projector(1 To 4, 1 To 4) As Single
  226. Private P(1 To 4, 1 To 4) As Single
  227. ' Create transformation matrices for perspective
  228. ' projection with:
  229. '       focus point             (focx, focy, focz)
  230. '       center of projection    (ex, ey, ez)
  231. '       up vector               <ux, uy, uz>
  232. Private Sub CreateMatrices(ByVal focx As Single, ByVal focy As Single, ByVal focz As Single, ByVal ex As Single, ByVal ey As Single, ByVal ez As Single, ByVal ux As Single, ByVal uy As Single, ByVal uz As Single)
  233. Dim sin1 As Single
  234. Dim cos1 As Single
  235. Dim sin2 As Single
  236. Dim cos2 As Single
  237. Dim sin3 As Single
  238. Dim cos3 As Single
  239. Dim A As Single
  240. Dim B As Single
  241. Dim C As Single
  242. Dim d1 As Single
  243. Dim d2 As Single
  244. Dim d3 As Single
  245. Dim up1(1 To 4) As Single
  246. Dim up2(1 To 4) As Single
  247.     ' Identity transformation.
  248.     m3Identity M(0).M
  249.     ' Translate the focus to the origin.
  250.     m3Translate M(1).M, -focx, -focy, -focz
  251.     A = ex - focx
  252.     B = ey - focy
  253.     C = ez - focz
  254.     d1 = Sqr(A * A + C * C)
  255.     sin1 = -A / d1
  256.     cos1 = C / d1
  257.     d2 = Sqr(A * A + B * B + C * C)
  258.     sin2 = B / d2
  259.     cos2 = d1 / d2
  260.     ' Rotate around the Y axis to place the
  261.     ' center of projection in the Y-Z plane.
  262.     m3Identity M(2).M
  263.     M(2).M(1, 1) = cos1
  264.     M(2).M(1, 3) = -sin1
  265.     M(2).M(3, 1) = sin1
  266.     M(2).M(3, 3) = cos1
  267.     ' Rotate around the X axis to place the
  268.     ' center of projection in the Z axis.
  269.     m3Identity M(3).M
  270.     M(3).M(2, 2) = cos2
  271.     M(3).M(2, 3) = sin2
  272.     M(3).M(3, 2) = -sin2
  273.     M(3).M(3, 3) = cos2
  274.     ' Apply the rotations to the UP vector.
  275.     up1(1) = ux
  276.     up1(2) = uy
  277.     up1(3) = uz
  278.     up1(4) = 1
  279.     m3Apply up1, M(2).M, up2
  280.     m3Apply up2, M(3).M, up1
  281.     ' Rotate around the Z axis to put the UP
  282.     ' vector in the Y-Z plane.
  283.     d3 = Sqr(up1(1) * up1(1) + up1(2) * up1(2))
  284.     sin3 = up1(1) / d3
  285.     cos3 = up1(2) / d3
  286.     m3Identity M(4).M
  287.     M(4).M(1, 1) = cos3
  288.     M(4).M(1, 2) = sin3
  289.     M(4).M(2, 1) = -sin3
  290.     M(4).M(2, 2) = cos3
  291.     ' Project.
  292.     m3PerspectiveXZ M(5).M, d2
  293.     ' Compute the projection all in one shot.
  294.     m3Project P, m3Perspective, ex, ey, ez, focx, focy, focz, ux, uy, uz
  295. End Sub
  296. ' Let the user change the location of the eye.
  297. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  298. Const Dtheta = PI / 20
  299. Const Dx = 0.25
  300. Dim inc As Single
  301.     If Shift And 1 Then
  302.         inc = Dx
  303.     Else
  304.         inc = -Dx
  305.     End If
  306.     Select Case KeyCode
  307.         Case vbKeyLeft
  308.             EyeTheta = EyeTheta - Dtheta
  309.             
  310.         Case vbKeyRight
  311.             EyeTheta = EyeTheta + Dtheta
  312.         
  313.         Case vbKeyUp
  314.             EyePhi = EyePhi - Dtheta
  315.         
  316.         Case vbKeyDown
  317.             EyePhi = EyePhi + Dtheta
  318.         
  319.         Case Asc("X")
  320.             UpX = UpX + inc
  321.         Case Asc("Y")
  322.             UpY = UpY + inc
  323.         Case Asc("Z")
  324.             UpZ = UpZ + inc
  325.         
  326.         Case Else
  327.             Exit Sub
  328.     End Select
  329.     ' Redraw the pictures.
  330.     DrawTheData
  331. End Sub
  332. Private Sub Form_Load()
  333.     ' Initialize the viewing parameters.
  334.     EyeR = 3
  335.     EyeTheta = PI * 0.35
  336.     EyePhi = PI * 0.1
  337.     ' Initialize projection parameters.
  338.     UpX = -1
  339.     UpY = 1.5
  340.     UpZ = 0
  341.     Cx = 2
  342.     Cy = 2.5
  343.     Cz = 3
  344.     Fx = 1
  345.     Fy = 1
  346.     Fz = 1
  347.     ' Create, project, and draw the data.
  348.     DrawTheData
  349. End Sub
  350. ' Draw all the pictures.
  351. Private Sub DrawTheData()
  352. Dim i As Integer
  353.     CreateData
  354.     CreateMatrices Fx, Fy, Fz, Cx, Cy, Cz, UpX, UpY, UpZ
  355.     ' Compute the projection matrix.
  356.     m3PProject Projector, m3Parallel, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  357.     For i = 0 To 5
  358.         TransformData M(i).M, FirstCube, NumSegments
  359.         SetPoints FirstCube, NumSegments
  360.         TransformData Projector, 1, NumSegments
  361.         DrawSomeData Pict(i), 1, NumSegments - 2, ForeColor, True
  362.         Pict(i).DrawWidth = 3
  363.         DrawSomeData Pict(i), NumSegments - 1, NumSegments - 1, vbRed, False
  364.         DrawSomeData Pict(i), NumSegments, NumSegments, vbGreen, False
  365.         Pict(i).DrawWidth = DrawWidth
  366.         Pict(i).Refresh
  367.     Next i
  368.     ' For the final view use the transformation
  369.     ' given by m3PerspectiveProjectionUp
  370.     CreateData
  371.     TransformData P, FirstCube, NumSegments
  372.     DrawSomeData PPict, FirstCube, NumSegments - 2, ForeColor, True
  373.     PPict.DrawWidth = 3
  374.     DrawSomeData PPict, NumSegments - 1, NumSegments - 1, vbRed, False
  375.     DrawSomeData PPict, NumSegments, NumSegments, vbGreen, False
  376.     PPict.DrawWidth = DrawWidth
  377.     PPict.Refresh
  378. End Sub
  379. ' Create the data.
  380. Private Sub CreateData()
  381.     ' Start with no data.
  382.     NumSegments = 0
  383.     ' Create the axes.
  384.     MakeSegment 0, 0, 0, 4, 0, 0    ' X axis.
  385.     MakeSegment 0, 0, 0, 0, 4, 0    ' Y axis.
  386.     MakeSegment 0, 0, 0, 0, 0, 4    ' Z axis.
  387.         
  388.     FirstCube = NumSegments + 1
  389.     ' Create the object to reflect.
  390.     MakeSegment -1, -1, -1, -1, -1, 3
  391.     MakeSegment -1, -1, 3, -1, 3, 3
  392.     MakeSegment -1, 3, 3, -1, 3, -1
  393.     MakeSegment -1, 3, -1, -1, -1, -1
  394.     MakeSegment 3, -1, -1, 3, -1, 3
  395.     MakeSegment 3, -1, 3, 3, 3, 3
  396.     MakeSegment 3, 3, 3, 3, 3, -1
  397.     MakeSegment 3, 3, -1, 3, -1, -1
  398.     MakeSegment -1, -1, -1, 3, -1, -1
  399.     MakeSegment -1, -1, 3, 3, -1, 3
  400.     MakeSegment -1, 3, 3, 3, 3, 3
  401.     MakeSegment -1, 3, -1, 3, 3, -1
  402.     ' Up vector.
  403.     MakeSegment Fx, Fy, Fz, Fx + UpX, Fy + UpY, Fz + UpZ
  404.     ' Center of projection.
  405.     MakeSegment Fx, Fy, Fz, Cx, Cy, Cz
  406. End Sub
  407.